package ThinkInJava;

public class ExceptionTest {

	public static String output = "";

	public static String foo(int i) throws Exception {
		try {
			if (i == 1) {
				throw new Exception(); // 若没有在catch语句中捕获异常，那么就会终止执行，但会先调用finally
				// return output+" kk"; //先执行output +“ kk” ，然后执行finally中的语句

			}
		} catch (Exception e) {
			output += "2";
			System.out.println("before return "); // 在return之前调用该语句
			return output;
		} finally {
			System.out.println("come from finally"); // 一旦要调用return那么先执行finally语句
			output += "3";
		}
		return output;
		// output += "4";
		// return output ;
	}

	public static void main(String[] args) {

		try {
			// foo(0);
			System.out.println(foo(1));
		} catch (Exception e) {
			output += "2";
		}
		System.out.println(output);
		System.out.println(Math.cos(Math.toRadians(90))); // toRadians表示转换为弧度，不准确
		System.out.println(Math.toDegrees(Math.PI)); // toDegress表示转换为角度
	}

	public void waitForSignal() {
		Object obj = new Object();
		synchronized (Thread.currentThread()) {
			try {
				obj.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			obj.notify();
		}
	}

	public void byteCalculate() {
		// byte和short类型的运算会自动的转换为int类型
		byte a1 = 2, a2 = 3, a3;
		short s1 = 2, s2 = 3, s3;
		a3 = (byte) (a1 + a2);
		s3 = (short) (s1 + s2);
		System.out.println(a3 + s3);
	}
}
